package com.jerry.huihome.module.find.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.jerry.huihome.R;
import com.jerry.huihome.base.BaseApplication;
import com.jerry.huihome.base.BaseViewHolder;
import com.jerry.huihome.base.MyBaseAdapter;
import com.jerry.huihome.module.find.entity.ForemanDynamic;
import com.jerry.huihome.utils.TextParser;
import com.jerry.huihome.views.ExpandGridView;
import com.jerry.huihome.views.RoundImageView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;

/**
 * Created by Administrator on 2016/5/13.
 */
public class ForemanDynamicAdapter extends MyBaseAdapter<ForemanDynamic.BodyBean.DynaItemsBean> {
    private Context context;
    private DisplayImageOptions options;
    private PopupWindow mPopupWindow;

    public ForemanDynamicAdapter(Context context, List<ForemanDynamic.BodyBean.DynaItemsBean> list) {
        super(context, list);
        this.context = context;
        options = new DisplayImageOptions.Builder().cacheInMemory(true)
                .cacheOnDisk(true)
                .imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2)
                .bitmapConfig(Bitmap.Config.ARGB_8888)
                .build();
    }

    @Override
    public int getLayoutId() {
        return R.layout.foreman_dynamic_item;
    }

    @Override
    public BaseViewHolder getViewHolder() {
        return new ViewHolder();
    }

    @Override
    public void bindViews(View view, BaseViewHolder viewHolder) {
        ForemanDynamic.BodyBean.DynaItemsBean item = getItem(viewHolder.position);
        ViewHolder mViewHolder = (ViewHolder) viewHolder;
        BaseApplication.getApplication().getImageLoader().displayImage(item.getAvatar(), mViewHolder.civForemanHeadImg, options);
        mViewHolder.tvNameForeman.setText(item.getUsername());
        mViewHolder.tvWorkStyle.setText(item.getPhase());
        mViewHolder.tvForemanResume.setText(item.getMsgtxt());
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd mm:HH:ss");
        String date = format.format(new Date(item.getOccurTime()));
        mViewHolder.tvForemanTime.setText(date);
        List<ForemanDynamic.BodyBean.DynaItemsBean.DynaImagesBean> dynaImages = item.getDynaImages();
        if (dynaImages == null) {
            mViewHolder.gvWorkImg.setVisibility(View.GONE);
        } else {
            List<String> listImage = new ArrayList<>();
            for (int i = 0; i < dynaImages.size(); i++) {
                listImage.add(dynaImages.get(i).getListImg());
            }
            GridViewListForemanAdapter imageAdapter = new GridViewListForemanAdapter(context, listImage);
            mViewHolder.gvWorkImg.setAdapter(imageAdapter);
        }
        List<ForemanDynamic.BodyBean.DynaItemsBean.AppraisesBean> appraises = item.getAppraises();
        if (appraises == null) {
            mViewHolder.ivForemanHeart.setVisibility(View.GONE);
            mViewHolder.llForemanHeart.setVisibility(View.GONE);
        } else {
            String name = "";
            mViewHolder.llForemanHeart.setVisibility(View.VISIBLE);
            mViewHolder.ivForemanHeart.setVisibility(View.VISIBLE);
            for (int i = 0; i < appraises.size(); i++) {
                name += (appraises.get(i).getUsername() + ";");
            }
            mViewHolder.tvForemanHeartName.setText(name);
        }
        List<ForemanDynamic.BodyBean.DynaItemsBean.CommentsBean> comments = item.getComments();
        if (comments == null) {
            mViewHolder.llForemanComment.setVisibility(View.GONE);
        } else {
            mViewHolder.llForemanComment.setVisibility(View.VISIBLE);
            mViewHolder.llForemanComment.removeAllViews();
            for (int i = 0; i < comments.size(); i++) {
                TextParser textParser = new TextParser();
                TextView textView = new TextView(context);
                textView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
                if (comments.get(i).getToUsername() == null) {
                    textParser.append(comments.get(i).getFromUsername(), 56, Color.BLUE);
                    textParser.append(comments.get(i).getMsgtxt(), 56, Color.BLACK);
                } else {
                    textParser.append(comments.get(i).getFromUsername(), 56, Color.BLUE);
                    textParser.append("回复", 56, Color.BLACK);
                    textParser.append((String) comments.get(i).getToUsername(), 56, Color.BLUE);
                    textParser.append(comments.get(i).getMsgtxt(), 56, Color.BLACK);
                }
                textParser.parse(textView);
                mViewHolder.llForemanComment.addView(textView);
            }
            mViewHolder.ivForemanMessage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mPopupWindow != null) {
                        initPopupWindow(v);
                    }
                }
            });
        }

    }

    private void initPopupWindow(View v) {
        View view = LayoutInflater.from(context).inflate(R.layout.layout_popupwindow, null);
        mPopupWindow = new PopupWindow(view, 600, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        int[] location = new int[2];
        v.getLocationOnScreen(location);
        // 点击空白区域和默认返回按钮取消弹出Window
        // 一般设置背景取消弹出Window
        ColorDrawable cd = new ColorDrawable(Color.TRANSPARENT);
        mPopupWindow.setBackgroundDrawable(cd);
        mPopupWindow.setTouchable(true);
        mPopupWindow.setOutsideTouchable(true);
        //显示在左边
        mPopupWindow.showAtLocation(v, Gravity.NO_GRAVITY,
                location[0] - mPopupWindow.getWidth(), location[1] - 15);
    }

    static class ViewHolder extends BaseViewHolder {
        @BindView(R.id.civ_foreman_head_img)
        RoundImageView civForemanHeadImg;
        @BindView(R.id.tv_name_foreman)
        TextView tvNameForeman;
        @BindView(R.id.tv_work_style)
        TextView tvWorkStyle;
        @BindView(R.id.tv_foreman_resume)
        TextView tvForemanResume;
        @BindView(R.id.gv_work_img)
        ExpandGridView gvWorkImg;
        @BindView(R.id.tv_foreman_time)
        TextView tvForemanTime;
        @BindView(R.id.tv_foreman_heart_name)
        TextView tvForemanHeartName;
        @BindView(R.id.ll_foreman_heart)
        LinearLayout llForemanHeart;
        @BindView(R.id.ll_foreman_comment)
        LinearLayout llForemanComment;
        @BindView(R.id.iv_foreman_heart)
        ImageView ivForemanHeart;
        @BindView(R.id.iv_foreman_message)
        ImageView ivForemanMessage;
    }
}
